package com.tjbank.cssys.web.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tjbank.cssys.domain.CkCreditOutCompanyData;
import com.tjbank.cssys.domain.CkCreditOutOneReservedData;
import com.tjbank.cssys.domain.CkCreditOutWuReservedData;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataLogMapper;
import com.tjbank.cssys.repository.CkCreditOutOneReservedDataMapper;
import com.tjbank.cssys.repository.CkCreditOutWuReservedDataMapper;
import com.tjbank.cssys.repository.CkOutsourcingCompanyMapper;
import com.tjbank.cssys.web.service.CkCreditOutOneReservedDataService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class CkCreditOutOneReservedDataServiceImpl implements CkCreditOutOneReservedDataService {
    private static Logger logger = LoggerFactory.getLogger(CkCreditOutOneReservedDataServiceImpl.class);
    @Autowired
    private CkOutsourcingCompanyMapper ckOutsourcingCompanyMapper;
    @Autowired
    private CkCreditOutOneReservedDataMapper ckCreditOutOneReservedDataMapper;
    @Autowired
    private CkCreditOutWuReservedDataMapper ckCreditOutWuReservedDataMapper;
    @Autowired
    private CkCreditOutCompanyDataLogMapper ckCreditOutCompanyDataLogMapper;

    @Override
    @Transactional
    public CommonResult uploadSaveOutOneReservedData(JSONObject jsonObject, String fileName) {
        fileName = jsonObject.get("fileName").toString();
        logger.info("---jsonObject:" + jsonObject);
        if (null == jsonObject || jsonObject.size() == 0) {
            return CommonResult.success(null, "上传数据是空");
        }
        JSONArray jsonArray = jsonObject.getJSONArray("datas");
        if (null == jsonArray || jsonArray.size() == 0) {
            return CommonResult.success(null, "上传数据是空");
        }
        List<CkCreditOutOneReservedData> oneReservedDataList = jsonArray.toJavaList(CkCreditOutOneReservedData.class);
        if (oneReservedDataList.size() == 0) {
            return CommonResult.success(null, "上传数据是空");
        }
        String resultCheck = checkWuCreditOut(oneReservedDataList);
        if (resultCheck != null) {
            return CommonResult.success(null, resultCheck);
        }
        oneReservedDataList = valueList(oneReservedDataList, "");
        Map<String, Object> mapResult = allotList(oneReservedDataList);
        Object oneOject = mapResult.get("1");
        if (null != oneOject) {
            List<CkCreditOutOneReservedData> oneReservedData = (List<CkCreditOutOneReservedData>) oneOject;
            if (null != oneReservedData && oneReservedData.size() > 0)
                ckCreditOutOneReservedDataMapper.insertBatchList(oneReservedData);
        }
        Object wuOject = mapResult.get("2");
        if (null != wuOject) {
            List<CkCreditOutWuReservedData> wuReservedData = (List<CkCreditOutWuReservedData>) wuOject;
            if (null != wuReservedData && wuReservedData.size() > 0)
                ckCreditOutWuReservedDataMapper.insertBatch(wuReservedData);
        }
        oneReservedDataList.clear();
        return CommonResult.success("上传成功");
    }


    public Map<String, Object> allotList(List<CkCreditOutOneReservedData> oneReservedDataList) {
        Map<String, Object> map = new HashMap<>();
        List<CkCreditOutWuReservedData> ckCreditOutWuReservedDataList = new ArrayList<>();
        Iterator<CkCreditOutOneReservedData> ckCreditOutCompanyDataIterator = oneReservedDataList.iterator();
        while (ckCreditOutCompanyDataIterator.hasNext()) {
            CkCreditOutOneReservedData ckCreditOutOneReservedData = ckCreditOutCompanyDataIterator.next();
            if (Integer.valueOf(ckCreditOutOneReservedData.getOldBatchNumber().substring(6, 8)) == 15) {
                CkCreditOutWuReservedData ckCreditOutWuReservedData = new CkCreditOutWuReservedData();
                BeanUtils.copyProperties(ckCreditOutOneReservedData, ckCreditOutWuReservedData);
                ckCreditOutWuReservedDataList.add(ckCreditOutWuReservedData);
                ckCreditOutCompanyDataIterator.remove();
            }
        }
        map.put("1", oneReservedDataList);
        map.put("2", ckCreditOutWuReservedDataList);
        return map;
    }

    /**
     * 验证数据集合
     *
     * @param list
     * @return
     */
    private String checkWuCreditOut(List<CkCreditOutOneReservedData> list) {
        StringBuffer stringBuffer = new StringBuffer("");
        Map<String, String> map = new HashMap<>();
        int countList = 0;
        for (CkCreditOutOneReservedData ckCreditOutOneReservedData : list) {
            int row = ++countList;
            if (ckCreditOutOneReservedData.getCardNumber() == null || "".equals(ckCreditOutOneReservedData.getCardNumber())) {
                stringBuffer.append(row + "-");
                stringBuffer.append("卡号是空,");
            }
            if (null == ckCreditOutOneReservedData.getOldBatchNumber() || "".equals(ckCreditOutOneReservedData.getOldBatchNumber()) || ckCreditOutOneReservedData.getOldBatchNumber().length() != 8) {
                stringBuffer.append(row + "-").append("批次号是空或长度不对");
            }
            if ("".equals(ckCreditOutOneReservedData.getOutCompanyName()) || null == ckCreditOutOneReservedData.getOutCompanyName()) {
                stringBuffer.append(row + "-");
                stringBuffer.append("公司名是空，");
            } else {
                Map<String, String> castType = ckOutsourcingCompanyMapper.getOutCompanyByName(ckCreditOutOneReservedData.getOutCompanyName());
                if (null == castType || castType.size() == 0) {
                    stringBuffer.append(row + "-");
                    stringBuffer.append("公司名称不正确");
                }
            }
           /* map.put("cardNumber", ckCreditOutOneReservedData.getCardNumber());
            map.put("batchNumber", ckCreditOutOneReservedData.getOldBatchNumber());
            map.put("outCompanyName", ckCreditOutOneReservedData.getOutCompanyName());
            map.put("zhiInstitution", ckCreditOutOneReservedData.getZhiInstitution());
            map.put("fenInstitution", ckCreditOutOneReservedData.getFenInstitution());
            int countResult = ckCreditOutCompanyDataLogMapper.getDataLogByParam(map);
            map.clear();
            if (countResult == 0) {
                stringBuffer.append(row + "-");
                stringBuffer.append("卡号，原批次号，公司名，支行机构，分行机构，填写有误");
            }*/
        }
        if (stringBuffer.length() > 1) {
            return stringBuffer.toString();
        }
        return null;
    }

    private List<CkCreditOutOneReservedData> valueList(List<CkCreditOutOneReservedData> list, String fileUploadName) {
        for (CkCreditOutOneReservedData ckCreditOutOneReservedData : list) {
            ckCreditOutOneReservedData.setBatchNumber(DateUtils.setBatchNumber(ckCreditOutOneReservedData.getOldBatchNumber()));
            ckCreditOutOneReservedData.setCurrentDate(new Date());
            if (Integer.valueOf(ckCreditOutOneReservedData.getOldBatchNumber().substring(6, 8)) == 15) {
                ckCreditOutOneReservedData.setAllotType("6");
            } else {
                ckCreditOutOneReservedData.setAllotType("3");
            }
            Map<String, String> castType = ckOutsourcingCompanyMapper.getOutCompanyByName(ckCreditOutOneReservedData.getOutCompanyName());
            ckCreditOutOneReservedData.setCaseType(castType.get("case_type"));
            ckCreditOutOneReservedData.setFileName(fileUploadName);
            ckCreditOutOneReservedData.setAllotConfirm("1");
        }
        return list;
    }
}
